package com.base_java.service.impl;


import com.base_java.common.Const;
import com.base_java.common.ServerResponse;
import com.base_java.dao.DeviceMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

@Component
public class CheckValidService {

    private static CheckValidService uniqueInstance = new CheckValidService();
    private CheckValidService(){}
    public static CheckValidService getInstance() {
        return uniqueInstance;
    }

    @Resource
//    public TaskMapper taskMapper;
    @Autowired
    public DeviceMapper deviceMapper;

//    public CheckValidService ta;
    public CheckValidService de;

    @PostConstruct
    public void init(){
//        ta=this;
        de=this;
//        ta.taskMapper= this.taskMapper;
        de.deviceMapper = this.deviceMapper;
    }

//        todo 需要用到反射优化减少代码量
    public ServerResponse<String> checkValid(String str, String tybe) {
        if (StringUtils.isNotBlank(tybe)) {
            //设备检索macid唯一性
            if (Const.MACID.equals(tybe)) {
                if (StringUtils.isBlank(str)) {
                    String tips="参数错误，需要传递"+tybe;
                    return ServerResponse.createByErrorMessage(tips);
                }
                int resultCount = de.deviceMapper.checkMacId(str);
                if (resultCount > 0) {
                    String errs=tybe+"已存在";
                    return ServerResponse.createByErrorMessage(errs);
                }
            }
            //任务检索任务名称唯一性
//            if (Const.TASKNAME.equals(tybe)) {
//                if (StringUtils.isBlank(str)) {
//                    String tips="参数错误，需要传递"+tybe;
//                    return ServerResponse.createByErrorMessage(tips);
//                }
//                int resultCount = ta.taskMapper.checkTaskName(str);
//                if (resultCount > 0) {
//                    String errs=tybe+"已存在";
//                    return ServerResponse.createByErrorMessage(errs);
//                }
//            }
        } else {
            return ServerResponse.createByErrorMessage("校验的type不能为空");
        }
        return ServerResponse.createBySuccessMessage("校验成功");

    }
}
